#pragma once


#include "COM.H"

#define WM_PROTOCL_TXT  (WM_USER+207)

enum  
{
    PS_NOT_READY = 0,
    PS_ALLOCATE,
    PS_READY,
};


enum {
    TM_SHARE,
    TM_SERVER = 10,
    TM_CLIENT = 20,
    TM_MAX = 30
};

typedef enum  te_PS_TMR_ID
{
    TM_RX_CHK,
    TM_START_PS,
    TM_RX_TIME_OUT,
    TM_TIME_OUT
}PS_TMR_ID;



enum 
{
    TEXT_ID_S_TX = 0,
    TEXT_ID_S_RX,
    TEXT_ID_C_TX,
    TEXT_ID_C_RX,
};

#define TM_PS_RX_CHK   1189

class CProtocol : public CWnd, public CCom
{
public:
    CProtocol();
    virtual ~CProtocol();
    void Config(INT RxLen, INT TxLen);
    virtual int Parse(void);
    int Start(void);
    virtual int Start(int PortNum, int baud);
    virtual int PackageSend(int text_id);
    void Stop(void);
    void SetPsTimer(PS_TMR_ID tmrid, DWORD tm);
    void KillPsTimer(PS_TMR_ID tmrid);
    void Free(void);
    void SetBuff(INT RxLen, INT TxLen);
    BYTE GetCheckSum(int isRx);
    static unsigned int CRC16(BYTE* Message, DWORD Len);
    void ShowText(BYTE * ptxt, INT len, INT txt_id);
    int GetTextInfo(char * ptr, INT &len);
    int GetTextInfo(CString &str);
protected :
    int m_state;

    BYTE * m_pRxbuf;
    BYTE * m_pTxbuf;
    int    m_RxLen;
    int    m_TxLen;
    INT m_RxBuffLen;  
    INT m_TxBuffLen;

    BYTE * m_ptrText;
    INT    m_nTxtLen;
    
public:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnTimer(UINT_PTR nIDEvent);
};
